// #include <iostream>
// #include <vector>
// #include <pthread.h>
// #include <string>
// #include <unistd.h>

// #define NUM 10

// using namespace std;

// class threaddata
// {
// public:
//     pthread_t tid;
//     char namebuffer[64];
// };

// void* start_routine(void* args)
// {
//     const char* name = static_cast<const char*>(args);
//     while(true)
//     {
//         cout<< "new thread create success,name: "<< name <<endl;
//         sleep(1);
//         //int *p = nullptr;
//         //*p = 100;
//     }
//     delete[] name;
//     return nullptr;
// }

// int main()
// {
//     // pthread_t id;

//     // int n = pthread_create(&id,nullptr,start_routine,(void*)"thread one");

//     // while(true)
//     // {
//     //     cout<< "new thread create success,name: main thread"<<endl;
//     //     sleep(1);
//     // }

//     vector<pthread_t> tids;

//     for(int i = 0;i < NUM ;i++)
//     {
//         //threaddata *td = new threaddata;
//         pthread_t tid;//线程tid
//         char* namebuffer = new char[64];
//         //char namebuffer[64];//为名字开辟一段栈区空间用来保存
//         snprintf(namebuffer,64,"%s:%d","thread",i);//给这段空间赋值0~9  thread:0 
//         pthread_create(&tid,nullptr,start_routine,namebuffer);//创建子线程调用start_routine函数打印名字
//     }

//     while(true)
//     {
//         cout<< "new thread create success,name: main thread"<<endl;
//         sleep(1);
//     }



//     return 0;
// }






#include <iostream>
#include <vector>
#include <pthread.h>
#include <string>
#include <unistd.h>
#include <cstdlib>
#include <cassert>

#define NUM 10

using namespace std;

class threaddata
{
public:
    int number;
    pthread_t tid;
    char namebuffer[64];
};

class returnthread
{
public:
    int _exit;
    int _code;
};

void* start_routine(void* args)
{
    threaddata *td = static_cast<threaddata *>(args);
    int cnt = 10;
    while(cnt)
    {
        //cout<<" td->tid: "<< td->tid <<endl;
        cout<< "new thread create success,name: "<< td->namebuffer << " cnt: " << cnt-- <<endl;
        sleep(1);
    }
    //delete td;
    returnthread* rt = new returnthread();
    rt->_code=10;
    rt->_exit=9;
    return (void*)100;
}


int main()
{
    vector<threaddata*> threads;

    for(int i = 0;i < NUM ;i++)
    {
        threaddata *td = new threaddata();
        td->number = i + 1;
        char* namebuffer = new char[64];
        snprintf(td->namebuffer,sizeof(td->namebuffer),"%s:%d","thread",i+1);
        pthread_create(&(td->tid),nullptr,start_routine,td);
        threads.push_back(td);
    }

    for(auto &iter : threads)
    {
        cout<<"create thread: "<<iter->namebuffer<<" : "<<iter->tid<<" success"<<endl;
    }

    sleep(5);

    for(int i = 0;i < threads.size() / 2;i++)
    {
        pthread_cancel(threads[i]->tid);
        cout<<"pthread_cancel: "<<threads[i]->namebuffer<<endl;
    }

    for(auto &iter:threads)
    {
        void *ret = nullptr;
        int n = pthread_join(iter->tid,(void**)&ret);
        assert(n == 0);
        cout<<"join : "<<iter->namebuffer<<" success,number: "<< (long long)ret <<endl;
        delete iter; 
    }

    cout<<"main thread quit "<<endl;

    while(true)
    {
        cout<< "new thread create success,name: main thread"<<endl;
        sleep(1);
    }

    return 0;
}

